<?php

namespace App\Http\Rpc\Message;

use App\Libs\Commons\MyMessage;
use App\Services\Message\PushService;
use Illuminate\Support\Facades\Validator;

class PushRpc
{

    /**
     * 短信发送接口
     * @param array $param
     * @throws \Exception
     */
    public static function sms($param = [])
    {

        $validator = Validator::make($param, [
            'mobile' => 'required',
            'text' => 'required'
        ]);

        if ($validator->fails()) {
            throw new \Exception($validator->errors()->first(), MyMessage::PARAM_LOSE);
        }

        $mobile = explode(',', $param['mobile']);
        foreach ($mobile as $v) {
            $param['mobile'] = $v;
            PushService::sms($param);
        }
    }

    /**
     * 小程序推送接口
     * @param array $param
     * @throws \Exception
     */
    public static function miniprogram($param = [])
    {

        $validator = Validator::make($param, [
            'template_id' => 'required',
            'touser' => 'required'
        ]);

        if ($validator->fails()) {
            throw new \Exception($validator->errors()->first(), MyMessage::PARAM_LOSE);
        }

        $touser = explode(',', $param['touser']);
        foreach ($touser as $v) {
            $param['touser'] = $v;
            PushService::miniprogram($param);
        }
    }

    /**
     * 企业微信推送接口
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public static function work($param = [])
    {

        $validator = Validator::make($param, [
            'touser' => 'required',
            'title' => 'required'
        ]);

        if ($validator->fails()) {
            throw new \Exception($validator->errors()->first(), MyMessage::PARAM_LOSE);
        }

        return PushService::work($param);
    }
}
